package com.atguigu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.atguigu.realtime.app.BaseAppV1;
import com.atguigu.realtime.bean.TradePaymentWindowBean;
import com.atguigu.realtime.common.Constant;
import com.atguigu.realtime.util.AtguiguUtil;
import com.atguigu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * com.atguigu.realtime.app.dws.Dws_07_DwsTradePaymentSucWindow
 *  交易域支付各窗口汇总表（练习）
 *  从 Kafka 读取交易域支付成功主题数据，统计支付成功独立用户数和首次支付成功用户数。
 */
public class Dws_07_DwsTradePaymentSucWindow extends BaseAppV1 {
    public static void main(String[] args) {
        // 需求启动的app: 1. 预处理  2. 下单详情  3. 支付成功详情
        new Dws_07_DwsTradePaymentSucWindow().init(
                4007,
                2,
                "Dws_07_DwsTradePaymentSucWindow",
                Constant.TOPIC_DWD_TRADE_PAY_DETAIL_SUC
        );
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        stream
                .map(JSON::parseObject)
                .keyBy(obj -> obj.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TradePaymentWindowBean>() {

                    private ValueState<String> lastPaySucState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastPaySucState = getRuntimeContext().getState(new ValueStateDescriptor<String>("lastPaySucState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject obj,
                                               Context ctx,
                                               Collector<TradePaymentWindowBean> out) throws Exception {
                        String lastPaySucDate = lastPaySucState.value();
                        long ts = obj.getLong("ts") * 1000;
                        String today = AtguiguUtil.toDate(ts);


                        Long paymentSucUniqueUserCount = 0L;

                        // 支付成功新用户数
                        Long paymentSucNewUserCount = 0L;
                        // 如果这次支付日期与最后一次支付日期不相等, 则这次是今天的第一次支付
                        if (!today.equals(lastPaySucDate)) {
                            // 这个用户的今天第一次支付成功
                            paymentSucUniqueUserCount = 1L;
                            lastPaySucState.update(today);

                            // 是否首次支付(是否是新支付用户)
                            if (lastPaySucDate == null) {
                                paymentSucNewUserCount = 1L;
                            }
                        }
                        //获取到一个支付人数就往外输出一个
                        if (paymentSucUniqueUserCount == 1) {
                            out.collect(new TradePaymentWindowBean("", "",
                                    "",
                                    paymentSucUniqueUserCount, paymentSucNewUserCount,
                                    ts
                            ));
                        }
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<TradePaymentWindowBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((bean, ts) -> bean.getTs())
                )
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(
                        new ReduceFunction<TradePaymentWindowBean>() {
                            @Override
                            public TradePaymentWindowBean reduce(TradePaymentWindowBean bean1,
                                                                 TradePaymentWindowBean bean2) throws Exception {
                                bean1.setPaymentSucUniqueUserCount(bean1.getPaymentSucUniqueUserCount() + bean2.getPaymentSucUniqueUserCount());
                                bean1.setPaymentNewUserCount(bean1.getPaymentNewUserCount() + bean2.getPaymentNewUserCount());

                                return bean1;
                            }
                        },
                        //所有窗口函数:AllWindowFunction
                        new AllWindowFunction<TradePaymentWindowBean, TradePaymentWindowBean, TimeWindow>() {
                            @Override
                            public void apply(TimeWindow window,
                                              Iterable<TradePaymentWindowBean> values,
                                              Collector<TradePaymentWindowBean> out) throws Exception {
                                TradePaymentWindowBean bean = values.iterator().next();

                                bean.setStt(AtguiguUtil.toDateTime(window.getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(window.getEnd()));

                                bean.setCurDate(AtguiguUtil.toDate(System.currentTimeMillis()));

                                out.collect(bean);
                            }
                        }
                )
                .map(bean -> {
                    SerializeConfig conf = new SerializeConfig();
                    conf.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
                    return JSON.toJSONString(bean, conf);
                })
                //.print();
                .addSink(FlinkSinkUtil.getDorisSink("gmall2022.dws_trade_payment_suc_window"));

    }
}
